package weibo4j.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import weibo4j.http.Response;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

public class BusStation extends WeiboResponse implements Serializable {

	private String sName;
	private double longitude;
	private double latitude;
	private long district;
	private String stationInfo;
	private String address;
	private String telephone;
	
	public BusStation()
	{

	}
	public BusStation(Response res)throws WeiboException, JSONException{
		super(res);
		JSONObject json=res.asJSONObject();
		constructJson(json);
	}
	
	public void constructJson(JSONObject json) throws JSONException {
		this.sName = json.getString("name");
		this.longitude = json.getDouble("longitude");
		this.latitude = json.getDouble("latitude");
		this.district = json.getLong("district");			
		this.stationInfo = json.getString("station_info");
		this.address = json.getString("address");
		this.telephone = json.getString("telephone");
	}


	public BusStation(JSONObject json)throws WeiboException, JSONException{
		constructJson(json);
	}
	public BusStation(String str) throws WeiboException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		constructJson(json);
	}

public String getSName() {
	return sName;
}
public void setSName(String name) {
	this.sName = name;
}

public double getLongitude() {
	return longitude;
}
public void setLongitude(double lon) {
	this.longitude = lon;
}

public double getLatitude() {
	return latitude;
}
public void setLatitude(double lat) {
	this.latitude = lat;
}

public long getDistrict() {
	return district;
}
public void setDistrict(long dist) {
	this.district = dist;
}

public String getStationInfo() {
	return stationInfo;
}
public void setStationInfo(String sInfo) {
	this.stationInfo = sInfo;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
public String getTelephone() {
	return telephone;
}
public void setTelephone(String phone) {
	this.telephone = phone;
}


@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((sName == null) ? 0 : sName.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	BusStation other = (BusStation) obj;
	if (sName != other.sName)
		return false;
	if (longitude != other.longitude)
		return false;
	if (latitude != other.latitude)
		return false;
	if (district != other.district)
		return false;
	if (stationInfo != other.stationInfo)
		return false;
	if (address != other.address)
		return false;
	if (telephone != other.telephone)
		return false;
	return true;
}

public static BusStationWapper constructWapperStation(Response res) throws WeiboException {
	JSONObject jsonStation = res.asJSONObject(); //asJSONArray();
	JSONArray stations = null;
	try {
		if(!jsonStation.isNull("stations")){				
			stations = jsonStation.getJSONArray("stations");
			int size = stations.length();
			List<BusStation> station = new ArrayList<BusStation>(size);
			for (int i = 0; i < size; i++) {
				station.add(new BusStation(stations.getJSONObject(i)));
			}
			long previousCursor = jsonStation.getLong("previous_curosr");
			long nextCursor = jsonStation.getLong("next_cursor");
			long totalNumber = jsonStation.getLong("total_number");
			String hasvisible = jsonStation.getString("hasvisible");
			return new BusStationWapper(station, previousCursor, nextCursor,totalNumber,hasvisible);
		}else{
			return null;
		}
		
	} catch (JSONException jsone) {
		throw new WeiboException(jsone);
	}
}

@Override
public String toString() {
	return "BusStation [name=" + sName + 
			", longitude="+ longitude + 
			", latitude="+latitude+
			", district=" + district + 
			", stationInfo=" + stationInfo + 			
			", address=" + address + 
			", telephone=" + telephone +"]";
	}
}
